package cn.amaake.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.sun.org.apache.bcel.internal.generic.NEW;

import cn.amaake.common.AmaakePage;
import cn.amaake.common.model.Class;
import cn.amaake.common.model.Course;
import cn.amaake.common.model.Result;
import cn.amaake.common.model.Student;
import cn.amaake.common.model.User;
import cn.amaake.service.ClassService;
import cn.amaake.service.ClassServiceImp;
import cn.amaake.service.ResultService;
import cn.amaake.service.ResultServiceImp;

public class ResultController extends Controller {

	@Inject(ClassServiceImp.class)
    private ClassService classService;
	
	@Inject(ResultServiceImp.class)
	private ResultService resultService;
	
	public void index(){
        List<Class> classList = classService.getClassList();
        setAttr("classList",classList);
        render("/views/result/index.html");
    }
	
	public void getStudentResultPage(){
        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");
        String name = getPara("name");
        String number = getPara("number");
        String class_id = getPara("class_id");
        String Kcid = getPara("Kcid");
        Page<Result> resultPage = resultService.getStudentResultPage(pageNumber,pageSize,name,number,class_id, Kcid);
        cn.amaake.common.Result result = new cn.amaake.common.Result();
        result.setData(new AmaakePage(resultPage));
        renderJson(result);
    }
	
	public void showAddStudentResult() {
		List<Course> courseList = Course.dao.getCourseList();
        setAttr("courseList",courseList);
        render("/views/result/add.html");
	}
	
	 public void addStudentResult(){
	        int id = getParaToInt("id");
	        int course_id = getParaToInt("course_id");
	        String pingshiresult = getPara("pingshiresult");
	        String shiyanresult = getPara("shiyanresult");
	        String qimoresult = getPara("qimoresult");
	        String zongresult = getPara("zongresult");
	        Student student = Student.dao.findById(id);
	        Result ifResult = Result.dao.getResult(id, course_id);
	        cn.amaake.common.Result resultdata = new cn.amaake.common.Result();
	        if(ifResult!=null) {
	        	resultdata.setErrorNo(500);
	        	resultdata.setErrorInfo("添加失败！该学生的所选科目已经添加过成绩，请查询后再添加！");
	        }else {
		        User user = getSessionAttr("usersession");
		        Result result = new Result();
		        result.setClassId(student.getClassId());
		        result.setStudentId(id);
		        result.setCourseId(course_id);
		        result.setPingshiresult(new BigDecimal(pingshiresult));
		        result.setShiyanresult(new BigDecimal(shiyanresult));
		        result.setQimoresult(new BigDecimal(qimoresult));
		        result.setZongresult(zongresult);
		        result.setUserId(user.getID());
		        result.setCreatetime(new Date());
		        boolean boo = result.save();
		        if(boo){
		        	resultdata.setErrorNo(0);
		        	resultdata.setErrorInfo("添加成功！");
		        }else{
		        	resultdata.setErrorNo(500);
		        	resultdata.setErrorInfo("添加失败！");
		        }
	        }
	        renderJson(resultdata);
	    }
	 
	    @Before(Tx.class)
	    public void deleteStudentResult(){
	        String ids = getPara("id");
	        String[] idss = ids.split(",");
	        cn.amaake.common.Result result = new cn.amaake.common.Result();
	        boolean boo = false;
	        for (String id : idss){
	            int idi = Integer.parseInt(id);
	            boo = Result.dao.deleteById(idi);
	        }
	        if(boo){
	            result.setErrorNo(0);
	            result.setErrorInfo("删除成功！");
	        }else{
	            result.setErrorNo(500);
	            result.setErrorInfo("删除失败！");
	        }
	        renderJson(result);
	    }
	    
	    public void showEditStudentResult(){
	        String id = getPara("id");
	        setAttr("id",id);
	        List<Course> courseList = Course.dao.getCourseList();
	        setAttr("courseList",courseList);
	        render("/views/result/edit.html");
	    }
	   
	    public void getStudentResult(){
	        String id = getPara("id");
	        Result result = Result.dao.findById(id);
	        Student student = Student.dao.findById(result.getStudentId());
	        result.put("number",student.getNumber());
	        result.put("name",student.getName());
	        cn.amaake.common.Result resultdata = new cn.amaake.common.Result();
	        resultdata.setData(result);
	        renderJson(resultdata);
	    }
	    
	    public void editStudentResult(){
	    	int id = getParaToInt("id");
	        int course_id = getParaToInt("course_id");
	        String pingshiresult = getPara("pingshiresult");
	        String shiyanresult = getPara("shiyanresult");
	        String qimoresult = getPara("qimoresult");
	        String zongresult = getPara("zongresult");
	        User user = getSessionAttr("usersession");
	        Result result = Result.dao.findById(id);
	        result.setPingshiresult(new BigDecimal(pingshiresult));
	        result.setShiyanresult(new BigDecimal(shiyanresult));
	        result.setQimoresult(new BigDecimal(qimoresult));
	        result.setZongresult(zongresult);
	        result.setUserId(user.getID());
	        result.setCreatetime(new Date());
	        boolean boo = result.update();
	        cn.amaake.common.Result resultdata = new cn.amaake.common.Result();
	        if (boo) {
	        	resultdata.setErrorNo(0);
	        	resultdata.setErrorInfo("更新成功！");
	        } else {
	        	resultdata.setErrorNo(500);
	        	resultdata.setErrorInfo("更新失败！");
	        }
	        renderJson(resultdata);
	    }
}
